<?php
require_once "weixin_info.php";//微信授权获取用户头像等信息
require_once "config.php";
if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $money = $_POST['money'];
	// $money = 0.01;
    $is_vip = $_POST['is_vip'];
    $type = $_POST['type'];
    $time = time();
    $order_num = $time.$_SESSION['Member_info'][0]['id'];

    require_once "WxPay.php";
    $WxPay = new WxPay($_SESSION['Member_info'][0]['openid']);
    $trade_no = time().mt_rand(1000000000,9999999999);
    // $trade_no = 11111;
    $Result = $WxPay->SubmitJsApiUnifiedOrder('吉事多', $trade_no, $money);
    $SignParam = $WxPay->signJsApiPrepayParam($Result['prepay_id']);

    //把这个放到支付成功后的订单添加
    $or_sql = "INSERT INTO dyspw_order(order_name, uid, order_money, order_type ,order_status,order_time,order_num,is_vip, recharge_type) VALUES ('个人充值', '".$_SESSION['Member_info'][0]['id']."', '".$money."','微信支付', -1, '".$time."','".$trade_no."','".$is_vip."','".$type."')";
    $add_order = mysqli_query($mysql, $or_sql);
    if(!empty($add_order)){
        $result['status'] = 'y';
		$result['data'] = $SignParam;
        $result['is_vip'] = $is_vip;
        exit(json_encode($result));
    }else{
        $result['status'] = 'n';
        $result['info'] = '表单提交失败，请重新提交';
        exit(json_encode($result));
    }
}



?>

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
	<title>充值</title>
	<link rel="stylesheet" href="css/core.css">
	<script src="js/jquery-2.2.4.min.js" type="text/javascript"></script>
	<script src="js/jquery.cookie.js" type="text/javascript"></script>
	<script src="js/layer/layer.js" type="text/javascript"></script>
	<script src="js/dsbridge.js" type="text/javascript"></script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cb5d49b07e0a753dc8ee27ba2c73495d";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body>

<section style="padding-bottom:60px;">
	<div class="main" style="width:90%;">
		<div class="recharge-user-info">
			<img src="<?php echo $_SESSION['Member_info'][0]['avatar'] ?>" />
			<p><?php echo $_SESSION['Member_info'][0]['username'] ?></p>
			<i>会员到期日：2020.10.10</i>
		</div>
	</div>
	<div class="recharge-list-box cl">
		<ul class="recharge-menu-box cl">
			<li class="cur">个人版</li>
			<li>企业版</li>
		</ul>
		<div class="recharge-content-box">
			<div class="content-items cl" style="display:block;">
				<div class="items-box lxby fl">
					<h2>包月</h2>
					<p class="real-money">￥<em>9.9</em></p>
					<input type="hidden" name="money" value="9.9">
					<input type="hidden" name="is_vip" value="2">
					<input type="hidden" name="type" value="month">
					<p class="market-money">38元/月</p>
                    <input type="radio" name="recharge_type" value="" style="display:none;">
				</div>
				<div class="items-box fr">
					<h2 style="margin-top:8px;">包年</h2>
					<p class="real-money">￥<em>60</em></p>
					<input type="hidden" name="money" value="60">
					<input type="hidden" name="is_vip" value="2">
					<input type="hidden" name="type" value="year">
                    <input type="radio" name="recharge_type" value="" style="display:none;">
				</div>
			</div>
			<div class="content-items cl">
				<div class="items-box lxby fl">
					<h2>包月</h2>
					<p class="real-money">￥<em>380</em></p>
					<p class="market-money">680元/月</p>
                    <input type="hidden" name="money" value="0.01">
					<input type="hidden" name="is_vip" value="3">
					<input type="hidden" name="type" value="month">
                    <input type="radio" name="recharge_type" value="" style="display:none;">
				</div>
				<div class="items-box fr">
					<h2 style="margin-top:8px;">包年</h2>
					<p class="real-money">￥<em>2280</em></p>
                    <input type="hidden" name="money" value="2280">
					<input type="hidden" name="is_vip" value="3">
					<input type="hidden" name="type" value="month">
                    <input type="radio" name="recharge_type" value="" style="display:none;">
				</div>
			</div>
		</div>
	</div>
	<div class="main" style="width:80%;">
		<div class="recharge-desc-box">
			<p>说明：</p>
			<p>个人版：支付9.9元成为个人会员即可享受商品展示等功能、个人主页展示；</p>
			<p>企业版：支付380元成为企业会员即可享受商品展示购买、个人中心展示商铺等功能。</p>
		</div>
		<div class="recharge-footer-box">
			<p><label><i></i>我已阅读并同意<a href="#">服务条款</a>及<a href="#">隐私协议</a></label></p>
			<button class="recharge-btn">确认充值</button>
		</div>
	</div>
</section>

<footer style="background-color:#FFF;">
    <ul class="cl">
        <li><a href="index.php">首页</a></li>
        <li><a href="fenlei.php">分类</a></li>
        <li><a href="baishe.php">拍</a></li>
        <li><a href="shopping.php">商城</a></li>
        <li><a href="member.php">我</a></li>
    </ul>
</footer>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">



$(function(){

    $(".recharge-menu-box li").click(function(){
		$(this).addClass("cur").siblings().removeClass("cur");
		$(".recharge-content-box").find(".content-items").hide().eq($(this).index()).show();
	});

	$(".content-items .items-box").click(function(){
		$(this).addClass("cur").siblings().removeClass("cur");
        $(this).find("input[name='recharge_type']").attr('checked',true);
	})

	$(".recharge-footer-box label").click(function(){
		if($(this).hasClass("cur")){
			$(this).removeClass("cur");
		}else{
			$(this).addClass("cur")
		}

	});

	$(".recharge-btn").click(function(){
		if($(".recharge-list-box").find(".cur").length == 0){
			return layer.open({content:"请选择充值类型",skin:'msg',time:3});
		}

		if(!$(".recharge-footer-box label").hasClass("cur")){
			return layer.open({content:"请同意服务条款及隐私协议",skin:'msg',time:3});
		}
        var money = $("input[name='recharge_type']:checked").parent().find('input[name="money"]').val();
        var is_vip = $("input[name='recharge_type']:checked").parent().find('input[name="is_vip"]').val();
        var type = $("input[name='recharge_type']:checked").parent().find('input[name="type"]').val();
        if(!money){
            return layer.open({content:"请选择充值金额",skin:'msg',time:3});
		}
		if(window.navigator.userAgent == 'AndroidNative'){
			$.ajax({
				url : "./api/appPay.php?action=recharge",
				type: "post",
				data:{
					"uid": '<?php echo $_SESSION['Member_info'][0]['id'] ?>',
					"money": money,
					"type": type,
					"isVip": is_vip,
					"payMethod": "aliPay"
				},
				dataType:'JSON',
				success:function(ret){
					if(!ret.code){
						dsBridge.call("aliPay", {
							data: ret.data
						});
					}else{
						alert(ret.message)
					}
				}
			})
		}else if(window.navigator.userAgent == 'IosNative'){
			$.ajax({
				url : "./api/appPay.php?action=recharge",
				type: "post",
				data:{
					"uid": '<?php echo $_SESSION['Member_info'][0]['id'] ?>',
					"money": money,
					"type": type,
					"isVip": is_vip,
					"payMethod": "aliPay"
				},
				dataType:'JSON',
				success:function(ret){
					if(!ret.code){
						dsBridge.call("aliPay", {
							data: ret.data
						});
					}else{
						alert(ret.message)
					}
				}
			})
		}else{
			$.ajax({
				url : "recharge.php",
				type: "post",
				data:{'money':money, 'is_vip':is_vip, 'type':type},
				dataType:'JSON',
				success:function(ret){

					if(ret.status == 'y'){
						WeixinJSBridge.invoke(
							'getBrandWCPayRequest',
							{
								"appId" : ret.data.appId,
								"timeStamp" : ret.data.timeStamp,
								"nonceStr" : ret.data.nonceStr,
								"package" : ret.data.package,
								"signType" : ret.data.signType,
								"paySign" : ret.data.paySign,
							},
							function(ret){
								if(ret.err_msg == "get_brand_wcpay_request:cancel"){
									layer.open({content:"您已取消了此次支付",skin:'msg',time:3});
								}else if(ret.err_msg == "get_brand_wcpay_request:fail"){
									layer.open({content:"支付失败，请重新尝试",skin:'msg',time:3});
								}else if(ret.err_msg == "get_brand_wcpay_request:ok"){
									layer.open({content:"支付成功,等待审核",skin:'msg',time:3});
									location.href = 'member.php?re_type=recharge';//跳转支付成功页面
								}else{
									layer.open({content:"未知错误"+ret.err_msg,skin:'msg',time:3});
								}
							}
						);
					}else{
						return layer.open({content:data.info,skin:'msg',time:3});
					}
				}
			})
		}
	});

});

</script>
</body>
</html>
<?php

?>
